package com.yangjie.autotest.dao;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.sql.Connection;
import java.util.List;

import javax.inject.Inject;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.SessionFactoryUtils;

public abstract class AbstractHbnDao<T extends Object> implements Dao<T>{
	@Inject
	private SessionFactory sessionFactory = null;
	private Class<T> domainClass;
	
	private Class<T> getDomainClass() {
		if(domainClass == null) {
			ParameterizedType type = (ParameterizedType)getClass().getGenericSuperclass();
			this.domainClass = (Class<T>)type.getActualTypeArguments()[0];
		}
		return domainClass;
	}
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	public Connection getConnection() throws Exception{
		return SessionFactoryUtils.getDataSource(getSessionFactory()).getConnection();
	}
	protected Session getSession() {
		return sessionFactory.getCurrentSession();
	}
	
	public T get(Serializable id) {
		return (T)getSession().get(getDomainClass(), id);
	}
	

	@Override
	public void create(Object t) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public T load(Serializable id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List getAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void update(Object t) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void delete(Object t) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void deleteById(Serializable id) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void deleteAll() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public long count() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public boolean exists(Serializable id) {
		// TODO Auto-generated method stub
		return false;
	}
}
